const router = require('koa-router')()

const {loginApi,registerApi,getAllUsersApi,getUsersApi,getUserApi}=require('../controller/user')

router.prefix('/users')


function handleRes(ctx,next,res){
     if(res.status===0){
       ctx.body=res
     }else{
       ctx.status=200
       ctx.body=res
     }
}


//用户操作
router.get('/getUser', async (ctx, next) =>{
  const {username,id}=ctx.query
  const res=await getUserApi({username,id,ctx});
  handleRes(ctx,next,res)
})
//获取所有的用户列表
router.get('/getUsers', async (ctx, next) =>{
  const res=await getUsersApi(ctx.query);
  handleRes(ctx,next,res)
})

router.get('/allUsers', async (ctx, next)=>{
    const res=await getAllUsersApi(ctx)
    handleRes(ctx,next,res)
})




//用户名登录
router.post('/login',async (ctx,next)=>{
  const { user_name, user_pwd } = ctx.request.body
  const res=await loginApi(user_name,user_pwd,ctx)
  handleRes(ctx,next,res)
})
//用户名注册
router.post('/register',async (ctx,next)=>{
    const { user_name, user_pwd } = ctx.request.body
    const res=await registerApi(user_name,user_pwd,ctx)
    handleRes(ctx,next,res)
})


module.exports = router
